﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;
public partial class frmUserList : BaseLoginFrom
{
    public frmUserList()
    {
        InitializeComponent();
        new TextBoxBindMonthCalendar(txtCreateDate);
        //Bind(false);
    }

    private void btSearch_Click(object sender, EventArgs e)
    {
        Bind();
    }

    private void Bind()
    {
        this.dgvUserList.AutoGenerateColumns = false;
        MemoryStream ms;

        DataTable dt;
        dt = new Users().List(txtUserName.Text, txtTelephone.Text, txtEmail.Text, txtCreateDate.Text, txtCreateDate.Text);
        ms = new MemoryStream();
        global::HealthExa.Properties.Resources.Edit.Save(ms, ImageFormat.Bmp);
        dt.Columns.Add(new DataColumn() { ColumnName = "Edit", DataType = typeof(byte[]), DefaultValue = ms.ToArray() });

        ms = new MemoryStream();
        global::HealthExa.Properties.Resources.Delete.Save(ms, ImageFormat.Bmp);
        dt.Columns.Add(new DataColumn() { ColumnName = "Delete", DataType = typeof(byte[]), DefaultValue = ms.ToArray() });

        ms = new MemoryStream();
        global::HealthExa.Properties.Resources.Child.Save(ms, ImageFormat.Bmp);
        dt.Columns.Add(new DataColumn() { ColumnName = "Child", DataType = typeof(byte[]), DefaultValue = ms.ToArray() });
        this.dgvUserList.DataSource = dt;
    }

    private void dgvUserList_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        DataGridView dgv = sender as DataGridView;
        DataRow dr = ((DataTable)(dgv.DataSource)).Rows[e.RowIndex];
        switch (e.ColumnIndex)
        {
            case 5:
                DB db = DB.getInstance();
                AddMdiParent(new frmDiagnosis(new Diagnosis
                {
                    PatientId = db.DB_Parse_String(dr["UserId"]),
                    Patient = new UserExtense
                    {
                        UserId = db.DB_Parse_String(dr["UserId"]),
                        UserName = db.DB_Parse_String(dr["UserName"]),
                        BirthDay = db.DB_Parse_String(dr["BirthDay"]),
                        Email = db.DB_Parse_String(dr["Email"]),
                        Telephone = db.DB_Parse_String(dr["Telephone"]),
                        BodyProperty = db.DB_Parse_String(dr["BodyProperty"]),
                        Status = db.DB_Parse_Number(dr["Status"]),
                        CreateDate = db.DB_Parse_String(dr["CreateDate"]),
                    }
                }));
                break;
            case 7:
                AddMdiParent(new frmDiagnosisList(dr["UserId"].ToString(), null, null, null));
                break;
        }
    }

    private void txtCreateDate_Enter(object sender, EventArgs e)
    {

    }

    private void mcd_DateSelected(object sender, DateRangeEventArgs e)
    {
        txtCreateDate.Text = e.Start.ToString("yyyy-MM-dd 00:00:00");
        ((Control)sender).Visible = false;

    }
}